﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using WeiboPlus.Models;
using WeiboPlus.Service;

namespace WeiboPlus.Sina
{
    public class SinaJTokenToFriendshipMapping : MappingBase<JToken, SNSFriendship>
    {
        public SinaJTokenToFriendshipMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSFriendship Map(JToken s)
        {
            try
            {
                var friendship = new SNSFriendship();

                if (s.SelectToken("source") != null)
                {
                    friendship.Source = new SNSFriendshipItem();
                    var source = s.SelectToken("source");
                    if (source.SelectToken("id") != null)
                    {
                        friendship.Source.ID = (long)source.SelectToken("id");
                    }
               
                    if (source.SelectToken("screen_name") != null)
                    {
                        friendship.Source.ScreenName = (string)source.SelectToken("screen_name");
                    }
                    if (source.SelectToken("following") != null)
                    {
                        friendship.Source.Following = (bool)source.SelectToken("following");
                    }
                    if (source.SelectToken("followed_by") != null)
                    {
                        friendship.Source.FollowBy = (bool)source.SelectToken("followed_by");
                    }
                }

                if (s.SelectToken("target") != null)
                {
                    friendship.Target = new SNSFriendshipItem();
                    var target = s.SelectToken("target");
                    if (target.SelectToken("id") != null)
                    {
                        friendship.Target.ID = (long)target.SelectToken("id");
                    }
                 
                    if (target.SelectToken("screen_name") != null)
                    {
                        friendship.Target.ScreenName = (string)target.SelectToken("screen_name");
                    }
                    if (target.SelectToken("following") != null)
                    {
                        friendship.Target.Following = (bool)target.SelectToken("following");
                    }
                    if (target.SelectToken("followed_by") != null)
                    {
                        friendship.Target.FollowBy = (bool)target.SelectToken("followed_by");
                    }
                }

                return friendship;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
                //throw;
            }

            //throw new NotImplementedException();
        }
    }
}
